using IQIGame.Onigao.Framework;
using IQIGame.Onigao.Game;

namespace IQIGame.Onigao.GamePlay
{
    public class UIRoleCreate : UIBaseWindow
    {
        /// <summary>
        /// 静态配置
        /// </summary>
        private static UICreateConfig _createConfig = new()
        {
            prefabName = nameof(UIRoleCreate),
            layer = EUILayer.Main
        };

        /// <summary>
        /// 创建UI的配置
        /// </summary>
        public override IUICreateConfig createConfig => _createConfig;

        #region Template Generate,don't modify
        protected partial class UIB_UIRoleCreate
        {
            #region ObjectBinding Generate 
            public IQIGame.Onigao.Framework.ExButton btn_Random { protected set; get; }
            public IQIGame.Onigao.Framework.ExButton btn_Next { protected set; get; }
            public IQIGame.Onigao.Framework.ExInputField input_Rolename { protected set; get; }
            public IQIGame.Onigao.Framework.ExToggle toggleMale { protected set; get; }
            public IQIGame.Onigao.Framework.ExToggle toggleFemale { protected set; get; }
            public virtual void InitBinding(ObjectBinding __binding)
            {
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExButton>("btn_Random", out var __tbv0);
                this.btn_Random = __tbv0;
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExButton>("btn_Next", out var __tbv1);
                this.btn_Next = __tbv1;
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExInputField>("input_Rolename", out var __tbv2);
                this.input_Rolename = __tbv2;
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExToggle>("toggleMale", out var __tbv3);
                this.toggleMale = __tbv3;
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExToggle>("toggleFemale", out var __tbv4);
                this.toggleFemale = __tbv4;
            }

            #endregion ObjectBinding Generate 
        }
        #endregion Template Generate,don't modify


        protected UIB_UIRoleCreate ui { get; set; }
        private EM_GameRoleSex RoleSex = EM_GameRoleSex.Female;

        protected override void BeforeInit()
        {
            ui = new UIB_UIRoleCreate();
            ui.InitBinding(this.csObjBind);
        }

        protected override void OnInit()
        {
            this.ui.btn_Next.onClick.AddListener(this.OnClickNewGameRole);
            this.ui.btn_Random.onClick.AddListener(this.OnRandName);

            this.ui.toggleFemale.onValueChanged.AddListener(this.OnFemaleValChged);
            this.ui.toggleMale.onValueChanged.AddListener(this.OnMaleValChged);

            this.VwTogInfo(this.ui.toggleFemale, EM_GameRoleSex.Female);
            this.VwTogInfo(this.ui.toggleMale, EM_GameRoleSex.Male);
            this.OnRandName();
        }

        protected override void OnShow(UIBaseData data)
        {

        }

        protected override void OnHide()
        {

        }

        void OnRandName()
        {
            this.ui.input_Rolename.text = LoadPlayerModule.Instance.GetRandRoleName();
        }

        void OnFemaleValChged(bool isOn)
        {
            if (!isOn) return;
            this.RoleSex = EM_GameRoleSex.Female;
        }

        void OnMaleValChged(bool isOn)
        {
            if (!isOn) return;
            this.RoleSex = EM_GameRoleSex.Male;
        }

        void VwTogInfo(ExToggle tog, EM_GameRoleSex roleSex)
        {
            var cfgOne = LoadPlayerModule.Instance.GetRoleInfo(roleSex);
            var exRaw = tog.GetComponentInChildren<ExRawImage>();
            this.SetRawImageTexture(exRaw, cfgOne.BustImage_fullPath);
        }

        void OnClickNewGameRole()
        {
            var name = this.ui.input_Rolename.text;
            if (string.IsNullOrEmpty(name))
            {
                return;
            }
            LoadPlayerModule.Instance.CreateRoleAsync(this.RoleSex, name).Forget();
        }

        protected override void OnDispose()
        {

        }
    }
}
